function InstallFunction(obj,functionName)
{
  obj[functionName] = function() { Request(functionName,arguments,'/rpc'); }
}

var endeavor_server = {};
//InstallFunction(server,'addEntity');
InstallFunction(endeavor_server,'addEndeavor');
InstallFunction(endeavor_server,'getEndeavor');
InstallFunction(endeavor_server,'getEndeavors');
InstallFunction(endeavor_server,'newEndeavor');
InstallFunction(endeavor_server,'clearCache');
InstallFunction(endeavor_server,'removeEndeavor');

function AddEndeavor(select_id)
{
  //$("#id-save-endeavor-btn").attr('disabled') = false;
  var save_btn = document.getElementById('id-save-endeavor-btn');
  save_btn.disabled = false;
  var quick_msg = document.getElementById('id-quick-manage-mode');
  quick_msg.innerHTML = ' -- Add Endeavor';  
  ResetEndeavorForm();
}

function AddEndeavorSuccess(response)
{
  if(response['short'])
  {
   $("#id-endeavor-accordion").html(response['short']);
  } else {
   $("#id-qk-work-form").html(response['work']);
   $("#id-qk-expr-form").html(response['expression']);
   $("#id-qk-man-form").html(response['manifestation']);
  }
  //alert(response['work']);
}

function ClearCache()
{
  endeavor_server.clearCache(ClearCacheSuccess);
}

function ClearCacheSuccess(response)
{
  if (response)
  {
    alert("Cache cleared");
  } 
}

function DisplayEndeavors()
{
  var endeavor_type = $("#id-endeavor-type-select").attr('value');
  endeavor_server.getEndeavors(endeavor_type,DisplayEndeavorsSuccess);
}

function DisplayEndeavorsSuccess(response)
{
  if(response)
  {
    //endeavor_select.add(new Option("None","NONE"));
	var options_str = '';
    for(var i=0;i<response.length;i++)
	{
	  options_str += '<option value="' + response[i]['key'] + '">' + response[i]['display_form'] + '</option>';
	}
	$('#id-existing-endeavors-select').html(options_str);
	//endeavor_select.style.display = "";
  } else {
    alert("Unable to retrieve Endeavors");
  }
}

function EditEndeavor()
{
  var endeavor_key = $("#id-existing-endeavors-select").attr('value');
  endeavor_server.getEndeavor(endeavor_key,EditEndeavorSuccess);
}

function EditEndeavorSuccess(response)
{
  if(response)
  {
    $("#id-title").attr('value',response['titleInfo']['title']);
	$("#id-title-key").attr('value',response['titleInfo']['key']);
	if(response['titleInfo']['subtitle'])
	{
	  $("#id-subtitle").attr('value',response['titleInfo']['subtitle']);
	}
	$("#id-title-display_label").attr('value',response['titleInfo']['display_form']);
	var subject_str = '';
	for(var i=0;i<response['concepts'].length;i++)
	{
	  subject_str += '<option value="' + response['concepts'][i]['concept_key'] + '">' + response['concepts'][i]['display_form'] + '</option>';
	}
	$("#id-concepts-select").html(subject_str);
	$("#id-access-address").attr('value',response['access_url']);
	$("#id-access-url-key").attr('value',response['access_url_key']);
	if(response['proxy_url'])
	{
	  $("#id-proxy-address").attr('value',response['proxy_url']);
	  $("#id-proxy-url-key").attr('value',response['proxy_url_key']);
	}
	$("#id-publisher-select").html('<option value="' + response['publisher_key'] + '">' + response['publisher_name'] + '</option>');
	if(response['description'])
	{
	 $("#id-endeavor-desc").attr('value',response['description']);
	 $("#id-desc-key").attr('value',response['description_key']);
	}
	if(response['is_subscription'])
	{
	  $("#is_subscription_yes").attr('checked',true);
	  $("#is_subscription_no").attr('checked',false);
	} else {
	  $("#is_subscription_yes").attr('checked',false);
	  $("#is_subscription_no").attr('checked',true);
	}
	if(response['is_trial'])
	{
	  $("#is_trial_yes").attr('checked',true);
	  $("#is_trial_no").attr('checked',false);
	} else {
	  $("#is_trial_yes").attr('checked',false);
	  $("#is_trial_no").attr('checked',true);
	}
	if(response['popularity'])
	{
	  $("#id-popularity").attr('value',response['popularity']);
	}
	var save_btn = document.getElementById('id-save-endeavor-btn');
    save_btn.disabled = false;
  } else {
    alert("Unable to retrieve endeavor information");
  }
}

function RemoveEndeavor()
{
 var endeavor = document.getElementById('id-existing-endeavors-select');
 var endeavor_key = endeavor.options[endeavor.options.selectedIndex].value;
 endeavor_server.removeEndeavor(endeavor_key,RemoveEndeavorSuccess);
}

function RemoveEndeavorSuccess(response)
{
  if (response)
  {
    alert(response);
  } else {
    alert("ERROR: Unable to remove endeavor.");
  }
}

function ResetEndeavorForm()
{
  $("#id-existing-endeavors-select").html('');
  $("#id-title").attr('value',"");
  $("#id-title-key").attr('value','');
  $("#id-subtitle").attr('value','');
  $("#id-title-display_label").attr('value','');
  $("#id-concepts-select").html('');
  $("#id-access-address").attr('value','');
  $("#id-access-url-key").attr('value','');
  $("#id-proxy-address").attr('value','');
  $("#id-proxy-url-key").attr('value','');
  $("#id-publisher-select").html('');
  $("#id-endeavor-desc").attr('value','');
  $("#id-desc-key").attr('value','');
  $("#is_subscription_no").attr('checked',false);
  $("#is_subscription_yes").attr('checked',false);
  $("#is_trial_no").attr('checked',false);
  $("#is_trial_yes").attr('checked',false);
  $("#id-popularity").attr('value','');
}

function SaveEndeavor() 
{
  var endeavor_data = new Array();
  endeavor_data[0] = $("#id-endeavor-type-select").attr('value');
  var titleInfo = new Array();
  titleInfo.push($("#id-title-key").attr('value'));
  titleInfo.push($("#id-title").attr('value'));
  titleInfo.push($("#id-subtitle").attr('value'));
  if(!$("#id-title-display_label").attr('value'))
  {
    titleInfo.push($("#id-title").attr('value'));
  } else {
    titleInfo.push($("#id-title-display_label").attr('value'))
  }
  endeavor_data[1] = titleInfo;
  var concepts = new Array();
  var concept_select = document.getElementById('id-concepts-select');
  for(var i=0;i<concept_select.options.length;i++)
  {
    concepts.push(concept_select.options[i].value);
  }
  endeavor_data[2] = concepts;
  var access_address = new Array();
  access_address.push($('#id-access-url-key').attr('value'));
  access_address.push( $("#id-access-address").attr('value'));
  endeavor_data[3] = access_address;
  var proxy_address = new Array();
  proxy_address.push($("#id-proxy-url-key").attr('value'));
  proxy_address.push($("#id-proxy-address").attr('value'));
  endeavor_data[4] = proxy_address;
  endeavor_data[5] = $("#id-publisher-select").attr('value');
  var description = new Array();
  description.push($("#id-desc-key").attr('value'));
  description.push($("#id-endeavor-desc").attr('value'));
  endeavor_data[6] = description;
  if($("#is_subscription_yes").attr('checked'))
  {
    endeavor_data[7] = 1;
  } else {
    endeavor_data[7] = 0;
  }
  if($("#is_trial_yes").attr('checked'))
  {
    endeavor_data[8] = 1;
  } else {
    endeavor_data[8] = 0;
  }  
  endeavor_data[9] = $("#id-popularity").attr('value');
  
  endeavor_data[10] = $("#id-existing-endeavors-select").attr('value');
  endeavor_data[11] = $("#id-language-select").attr('value');
  endeavor_server.newEndeavor(endeavor_data,SaveEndeavorSuccess);
}

function SaveEndeavorSuccess(response)
{
  alert(response);
  ResetEndeavorForm();
  var save_btn = document.getElementById('id-save-endeavor-btn');
  save_btn.disabled = true;
  var quick_msg = document.getElementById('id-quick-manage-mode');
  quick_msg.innerHTML = ' -- Endeavor Saved';
}